<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="../../static/font-awesome-4.7.0/css/font-awesome.min.css" >
    <link rel="stylesheet" href="../../static/vui/css/vui.css">

    <script type="text/javascript" src="../../static/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../../static/layer/layer.js"></script>
    <script type="text/javascript" src="../../static/laydate/laydate.js"></script>
    <script type="text/javascript" src="../../static/vui/js/treegrid.js"></script>
    <script type="text/javascript" src="../../static/vui/js/vui.js"></script>
    <script type="text/javascript" src="../../static/js/common.js"></script>
</head>
<body>

<div id="formArea">
    <table class="table-top">
        <tr>
            <td>
                <label class="label">单据日期</label>
            </td>
            <td>
                <input type="text" n="date" class="input search-date" name="dateStart" autocomplete="off"/>&nbsp;-&nbsp;
                <input type="text" n="date" class="input search-date" name="dateEnd" autocomplete="off"/>
            </td>
            <td>
                <button class="button" id="query"><span>查询</span></button>
            </td>
        </tr>
    </table>
</div>
<div id="table"></div>

<script>
    $(function(){
        $('#query').click(function(){
            app.request({
                url:'report/profit',
                data:app.serializeForm(),
                success:function (resp) {
                    setData(resp.rows[0]);
                }
            });
        });

        function setData(row){
            //销售收入
            setDataIndex(row.xssr,2);
        }

        function setDataIndex(price,index){
            $('#table .vui-table-row').eq(index).find('div[field="price"]').html(price);
        }

        $('#table').treegrid({
            root:-1,
            min:true,width:560,pagination:false,tableCss:{"margin":"0"},
            columns:[
                {field:'name',title:'项目',width:200},
                // {field:'number',title:'行次',width:20},
                {field:'price',title:'金额',width:180,n:'money'}
            ]
        });

        $('#table').treegrid("loadData",data);

        var nowDate = app.time2str(new Date());
        dateStart = laydate.render({
            elem: 'input[name="dateStart"]',value:nowDate.substr(0,8)+'01',max:nowDate,
            done: function(value,date){
                if(value){
                    console.log(date);
                    date.month=date.month-1;
                    dateEnd.config.min = date;
                }else{
                    dateEnd.config.min=dateStart.config.min;
                }
            }
        });
        dateEnd = laydate.render({
            elem: 'input[name="dateEnd"]',value:nowDate,min:nowDate.substr(0,8)+'01',
            done: function(value,date){
                if(value){
                    date.month=date.month-1;
                    dateStart.config.max = date;
                }else{
                    dateStart.config.max=dateEnd.config.max;
                }
            }
        });

        $('#reset').click(function () {
            $('#formArea input').val('');

            var endDate = app.time2str(new Date());
            var startDate = endDate.substr(0,8)+'01';
            $('input[name="dateStart"]').val(startDate);

            dateEnd.config.min = {year:getYear(startDate),month:getMonth(startDate),date:getDate(startDate)};

            $('input[name="dateEnd"]').val(endDate);
            dateStart.config.max = {year:getYear(endDate),month:getMonth(endDate),date:getDate(endDate)};


        });

        function getYear(dateStr){
            return parseInt(dateStr.split('-')[0]);
        }

        function getMonth(dateStr){
            return parseInt(dateStr.split('-')[1])-1;
        }

        function getDate(dateStr){
            return parseInt(dateStr.split('-')[2]);
        }
    });

    var data = [
        {id: "100", parentId: "-1", name: "主营业务",number:1},
        {id: "110", parentId: "100", name: "销售收入",number:2},
        {id: "120", parentId: "100", name: "销售成本",number:3},
        {id: "130", parentId: "100", name: "盘盈盘亏",number:4},
        {id: "131", parentId: "130", name: "盘盈",number:5},
        {id: "132", parentId: "130", name: "盘亏",number:6},
        {id: "200", parentId: "-1", name: "主营业务利润",number:7},
        {id: "300", parentId: "-1", name: "其它业务利润",number:8},
        {id: "400", parentId: "-1", name: "净利润",number:9}
    ];




</script>

</body>
</html>